package com.yb.xczx.cloud.auth.bean;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/18 13:51
 * FileName: XczxAbstractAuthenticationToken
 * Description:
 */
public abstract class XczxAbstractAuthenticationToken extends AbstractAuthenticationToken {
    /**
     * Creates a token with the supplied array of authorities.
     *
     * @param authorities the collection of <tt>GrantedAuthority</tt>s for the principal
     *                    represented by this authentication object.
     */

    private XczxSysUser xczxSysUser;

    private String loginType;

    private String loginAccessToken;

    private String oauth2AccessToken;

    private String jwtToken;

    public XczxAbstractAuthenticationToken(XczxSysUser xczxSysUser) {

        super(null);
        super.setAuthenticated(false);
        this.xczxSysUser=xczxSysUser;
        this.loginType=getLoginType();
    }

    public XczxAbstractAuthenticationToken(XczxSysUser xczxSysUser, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        super.setAuthenticated(true);
        this.xczxSysUser=xczxSysUser;
        this.loginType=getLoginType();
    }




    @Override
    public Object getCredentials() {
        return xczxSysUser.getSysUser().getPassword();
    }

    @Override
    public Object getPrincipal() {
        return xczxSysUser;
    }


    public void setXczxSysUser(XczxSysUser xczxSysUser) {
        this.xczxSysUser = xczxSysUser;
    }

    public XczxSysUser getXczxSysUser() {
        return xczxSysUser;
    }

    public void setLoginType(String loginType) {
        this.loginType = loginType;
    }

    public abstract String getLoginType();

    @Override
    public String getName() {
        return super.getName();
    }

    public String getUsername(){
        return this.getName();
    }

    public Integer getUserId(){
        return  xczxSysUser.getSysUser().getUserId();
    }



    public void setLoginAccessToken(String loginAccessToken) {
        this.loginAccessToken = loginAccessToken;
        this.xczxSysUser.setLoginAccessToken(loginAccessToken);
    }

    public String getLoginAccessToken() {
        return !StringUtils.hasText(loginAccessToken)?this.xczxSysUser.getLoginAccessToken():loginAccessToken;
    }

    public void setOauth2AccessToken(String oauth2AccessToken) {
        this.oauth2AccessToken = oauth2AccessToken;
        this.xczxSysUser.setOauth2AccessToken(loginAccessToken);
    }

    public String getOauth2AccessToken() {
        return  !StringUtils.hasText(oauth2AccessToken)?this.xczxSysUser.getOauth2AccessToken():oauth2AccessToken;
    }

    public void setJwtToken(String jwtToken) {
        this.jwtToken = jwtToken;
    }

    public String getJwtToken() {
        return jwtToken;
    }
}
